#!/usr/bin/env node


// console.log(process.argv);

// console.log('arguments number:', process.argv.length);
// console.log('arguments:', process.argv);
//正式的参数从下标为2的开始

// const expression = process.argv[2];
// console.log(expression + '=%d', eval(expression));



const args = process.argv.slice(2);


// 检查参数数量和内容，打印使用说明
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
  console.log('Usage: ./02-calc.js <expression>');
  console.log('Example:./02-calc.js 2+3*5');
  process.exit(0);
}

// 检查参数数量，忽略多余的参数
if (args.length > 3) {
  console.log('Warning: More than 3 arguments provided. Only the first 3 will be considered.');
}

try {
  const expression = args.join(' ');
  const result = eval(expression);
  console.log(`Result: ${result}`);
} catch (error) {
  console.error('Error: Invalid mathematical expression.');
}